package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.UserInfoBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.CustomDialog;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//登录
public class UserLogInActivity extends BaseActivity {
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.yhxy_tv)
    TextView yhxyTv;
    @BindView(R.id.btn_res)
    TextView btnRes;
    @BindView(R.id.btn_zhuce)
    TextView btnZhuce;
    @BindView(R.id.btn_wangji)
    TextView btnWangji;
    @BindView(R.id.btn_weixin)
    ImageView btnWeixin;
    @BindView(R.id.btn_xieyi)
    TextView btnXieyi;

    private CustomDialog.Builder builder;
    private CustomDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userlogin);
        ButterKnife.bind(this);

        if (BcUtils.getAgreementAgreed().equals(""))
            initDialog();
    }

    private void initDialog() {
        builder = new CustomDialog.Builder(this)
                .cancelTouchout(false)
                .view(R.layout.dialog_userlogin_policy)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y550)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_userlogin_policy_protocol, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startAction(UserLogInProtocolActivity.class);
                    }
                })
                .addViewOnclick(R.id.dialog_userlogin_policy_policy, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startAction(UserLogInPolicyActivity.class);
                    }
                })
                .addViewOnclick(R.id.dialog_userlogin_policy_no, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                })
                .addViewOnclick(R.id.dialog_userlogin_policy_yes, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BcUtils.setAgreementAgreed("1");
                        dialog.dismiss();
                    }
                });

        dialog = builder.build();
        dialog.show();
    }

    @OnClick({R.id.btn_res, R.id.btn_zhuce, R.id.btn_wangji, R.id.btn_xieyi, R.id.activity_userlogin_policy})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_res:
                volleyLogin();
                break;
            case R.id.btn_zhuce:
                intent = new Intent(this, RegisteredActivity.class);
                intent.putExtra("title", "注册");
                startActivity(intent);
//                startAction(UserZC_sms_Activity.class);
                break;
            case R.id.btn_wangji:
                intent = new Intent(this, RegisteredActivity.class);
                intent.putExtra("title", "忘记密码");
                startActivity(intent);
//                startAction(UserPWD_sms_Activity.class);
                break;
            case R.id.btn_xieyi:
                startAction(UserLogInProtocolActivity.class);
                break;
            case R.id.activity_userlogin_policy:
                startAction(UserLogInPolicyActivity.class);
                break;
        }
    }


    ///*
//* 密码登录
//* */
    public void volleyLogin() {

        final Gson mGson = new Gson();
        String url = Constants.USER_LOGIN;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("登录返回", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        JSONObject dataInfo = json.getJSONObject("data");
                        String data = json.getString("data");
                        UserInfoBean userInfoBean = mGson.fromJson(data, UserInfoBean.class);
                        BcUtils.setUserInfo(userInfoBean);
                        BcUtils.setUid(userInfoBean.getUserId());
                        BcUtils.setToken(userInfoBean.getToken());
                        BcUtils.setPhone(userInfoBean.getUserPhone());
                        BcUtils.setSignState(userInfoBean.getUserState());
                        BcUtils.setuserRent(userInfoBean.getUserRent());
                        BcUtils.setuserDeposit(userInfoBean.getUserDeposit());
                        BcUtils.setbindingElectricBicycle(userInfoBean.getBindingElectricBicycle());
//                        startAction(MainActivity.class);
                        startAction(MainNewActivity.class);
                        finish();
                    } else {
                        Log.i("站好", "账号");
                        toast(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userPhone", editPhone.getText().toString());
                    json_url.put("userPassword", editPwd.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }
}
